/**
@file	
	mmoSocket.h
@brief	
	Portable declaration of Socket.	
@author	
	GengYong
@revision
	2005/09/21 * Created by GengYong
*/

#ifndef MMONETWORK_HEADERFILE_MMOSOCKET_H
#define MMONETWORK_HEADERFILE_MMOSOCKET_H

//----------------------------------------------------------------------------
// Envirment defination.
//----------------------------------------------------------------------------
#ifdef _WIN32
	#define __WINSOCK__ 
	#define SOCKET_API TEXT("WinSock2")
#else
	#define __BSD_SOCKETS__ 
	#define SOCKET_API TEXT("Sockets")
#endif

//----------------------------------------------------------------------------
// WinSock includes.
//----------------------------------------------------------------------------
#if defined(__WINSOCK__)
#ifdef _XBOX
	#include <xtl.h>
#else
	#include <winsock2.h>
#pragma warning(disable:4127)
	#include <ws2tcpip.h>
#pragma warning(default:4127)
#endif
#endif 

//----------------------------------------------------------------------------
// BSD socket includes.
//----------------------------------------------------------------------------
#if defined(__BSD_SOCKETS__)
	#include <stdio.h>
	#include <unistd.h>
	#include <sys/types.h>
	#include <sys/socket.h>
	#include <sys/uio.h>	
	#include <sys/ioctl.h>
	#include <sys/time.h>
	#include <netinet/tcp.h>
	#include <netinet/in.h>
	#include <arpa/inet.h>
	#include <errno.h>	
	#include <fcntl.h>

	// Handle glibc < 2.1.3
	#ifndef MSG_NOSIGNAL
	#define MSG_NOSIGNAL 0x4000
	#endif
#endif


//----------------------------------------------------------------------------
// Provide WinSock definitions for BSD sockets.
//----------------------------------------------------------------------------
#if defined(__WINSOCK__)
	typedef INT					SOCKLEN;
	#define GCC_OPT_INT_CAST
	#define MSG_NOSIGNAL		0
    #define UDP_ERR_PORT_UNREACH WSAECONNRESET
#endif

#if defined(__BSD_SOCKETS__)
	typedef int					SOCKET;
	typedef struct hostent		HOSTENT;
	typedef in_addr				IN_ADDR;
	typedef struct sockaddr		SOCKADDR;
	typedef struct sockaddr_in	SOCKADDR_IN;
	typedef struct linger		LINGER;
	typedef struct timeval		TIMEVAL;
	typedef socklen_t			SOCKLEN;

	#define INVALID_SOCKET  (SOCKET)(~0)
	#define SOCKET_ERROR            (-1)

    #define UDP_ERR_PORT_UNREACH ECONNREFUSED

	#define WSAEWOULDBLOCK		EWOULDBLOCK	
	#define WSAENOTSOCK			ENOTSOCK
	#define WSATRY_AGAIN		TRY_AGAIN
	#define WSAHOST_NOT_FOUND	HOST_NOT_FOUND
	#define WSANO_DATA			NO_ADDRESS	
    #define WSAECONNRESET		ECONNRESET
	
	#define LPSOCKADDR			sockaddr*

	#define closesocket			close
	#define ioctlsocket			ioctl
	#define WSAGetLastError()	errno

	#define TEXT(a)				(a)

	#define GCC_OPT_INT_CAST	(unsigned long*)
#endif


//----------------------------------------------------------------------------
//	IP address macros.
//----------------------------------------------------------------------------
#if defined (__WINSOCK__)
	#define IPBYTE(sin_addr,n) sin_addr.S_un.S_un_b.s_b##n
#elif defined(__BSD_SOCKETS__)
	#define IPBYTE(sin_addr,n) ((unsigned char*)&sin_addr.s_addr)[n-1]
#endif


#endif	//#ifndef MMONETWORK_HEADERFILE_MMOSOCKET_H
